package com.XinYun.Library.modules.admin.bookCRUD.dao;

import cn.hutool.core.util.StrUtil;
import com.XinYun.Library.modules.admin.bookCRUD.entity.Book;
import com.XinYun.Library.modules.admin.bookCRUD.entity.BookClassInfo;
import com.XinYun.Library.modules.admin.bookCRUD.entity.BookCntInfo;
import com.XinYun.Library.modules.admin.bookCRUD.entity.BookComparator;
import com.XinYun.Library.modules.admin.bookCRUD.service.BookService;
import com.XinYun.Library.modules.admin.borrowMsg.entity.BorrowMsg;
import com.XinYun.Library.modules.admin.common.BasicDao;
import com.XinYun.Library.modules.admin.common.SqlMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BooksDao extends BasicDao {
    /*获取大类书籍信息*/
    public List<BookCntInfo> getBookClassInfo(SqlMapper sqlMethod, int pagesize, int pageindex, String isSearch){
        String sql = sqlMethod.getSql();
        if (!StrUtil.isEmpty(isSearch)){
            sql += isSearch;
        }
        return pageBeanSelect(sql,pagesize,pageindex, BookCntInfo.class);
    }

    /*获取大类书籍数量*/
    public Long getDataNum(SqlMapper sqlMethod, String isSearch){
        String sql = sqlMethod.getSql();
        if (!StrUtil.isEmpty(isSearch)){
            sql += isSearch;
        }
        List<Long> num = dqlForScalar(sql,Long.class);
        return Long.valueOf(num.get(0));
    }

    /**
     * 给某本书填多少本
     * @param bookName
     * @param num
     * @throws IllegalAccessException
     */
    private void addBook(String bookName, int num) throws IllegalAccessException {
        //设置书
        List<Book> appendBooks = new ArrayList<>();
        //无可覆盖追尾添加
        int lastBid = getLastBid();
        for (int i = 0; i < num; i++) {
            //查询不可视数据
            String sql = "SELECT MIN(`bid`) FROM book_id WHERE `visible`=0;";
            String minBid =  (String) dqlForScalar(sql,String.class).get(0);
            //存在不可视优先覆盖就覆盖
            if(minBid != null){
                String sql1 = "UPDATE book_id SET `bookname`= ?,`state`=1,`visible`=1 WHERE `bid`= ?;";
                dml(sql1,bookName,minBid);
            }else {
                //追尾添加
                Book book = new Book(bookName, String.valueOf(lastBid + 1),1,1);
                lastBid += 1;
                appendBooks.add(book);
            }
        }
        addBean("book_id",appendBooks);
    }

    /**
     * 添加新的一种书
     * @throws IllegalAccessException
     */
    public void addBookClass(List<BookClassInfo> bci) throws IllegalAccessException {
        addBean("books_info",bci);
        //添加具体书到id
        for (BookClassInfo bclazz : bci) {
            String bookname = bclazz.getBookname();
            Integer num = bclazz.getNum();
            addBook(bookname,num);
        }
    }

    /**
     * 修改图书类信息
     * @param bookName
     * @param newIntro
     * @param newNum
     * @param newCategory
     * @return
     * @throws IllegalAccessException
     */
    public void alterBookClassInfo(String bookName,String newIntro,Integer newNum,String newCategory) throws IllegalAccessException {
        //具体修改书
        String sql2 = "SELECT num FROM books_info WHERE `bookname`= ?;";
        //修改本
        Integer lastNum = (Integer)dqlForScalar(sql2, Integer.class, bookName).get(0);
        if(newNum > lastNum){
            addBook(bookName,newNum - lastNum);
        }else {
            //找到第一本书
            String sql3 = "SELECT MIN(`bid`) FROM book_id WHERE `bookname`= ?;";
            Integer minBid =  Integer.valueOf((String) dqlForScalar(sql3,String.class,bookName).get(0));
            for (int i = 0; i < (lastNum - newNum); i++) {
                //改为不可视
                String sql4 = "UPDATE book_id SET `visible`=0 WHERE `bid`= ?;";
                dml(sql4,minBid);
                minBid++;
            }
        }
        String sql1 = "UPDATE books_info SET `num`= ?,`intro`= ?,`category`= ? WHERE bookname= ?;";
        dml(sql1,newNum,newIntro,newCategory,bookName);
    }

    public void deleteBookClass(String bookName){
        //从类中删除
        String sql1 = "DELETE FROM books_info WHERE `bookname`= ?;";
        dml(sql1,bookName);
        //从库中隐藏
        String sql2 = "UPDATE book_id SET `visible`=0 WHERE `bookname`= ?;";
        dml(sql2,bookName);
    }

    /**
     * 获得当前所有书的数量也就是id
     * @return 总书数
     */
    public Integer getLastBid(){
        String sql = "SELECT MAX(`bid`) FROM book_id;";
        List<String> list = dqlForScalar(sql,String.class);
        //获取最后的id
        return Integer.valueOf(list.get(0));
    }

    /**
     * 获得对应状态的书的总数也就是id
     * @return 根据请求状态，返回该状态书数量
     *  -1 表示请求状态不存在
     */
    public long getStateNum(Integer state){
        switch (state){
            case 1: case 0: case -1:
                String sql = "SELECT COUNT(bid) FROM book_id WHERE `state` = ?;";
                List<Long> list = dqlForScalar(sql, String.class,state);
                //获取最后的id
                return list.get(0);
            default:break;
        }
        return -1;
    }

    public BookComparator getCompareData(String bookname){
        String sql = "SELECT COUNT(`state`) AS `saved`,count(`bid`) AS `total` " +
                "FROM books_info INNER JOIN book_id " +
                "ON (books_info.bookname = book_id.bookname) WHERE books_info.bookname= ?;";
        return (BookComparator) dqlForBeanRow(sql, BookComparator.class,bookname);
    }
}

